package com.nft.service.school.controller;

import com.alibaba.fastjson.JSONObject;
import com.nft.constant.response.ResponseConstants;
import com.nft.paging.Paginator;
import com.nft.po.NftResponse;
import com.nft.service.school.dto.BookContentDto;
import com.nft.service.school.dto.BookDetailDto;
import com.nft.service.school.req.BookReq;
import com.nft.service.school.req.UserBookReq;
import com.nft.service.school.resp.BookResp;
import com.nft.service.school.resp.SysUserResp;
import com.nft.service.school.resp.UserBookDetailResp;
import com.nft.service.school.service.BookManagerService;
import com.nft.utils.WxCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/book")
@Api(tags = "图书手机端模块")
public class BookController {
    @Autowired
    private BookManagerService bookManagerService;

    @ApiOperation(value="获取年级信息")
    @RequestMapping(value = "/getGrade", method = RequestMethod.GET)
    public NftResponse<List<String>> getGrade() {
        return bookManagerService.getGrade();
    }

    @ApiOperation(value="获取科目信息")
    @RequestMapping(value = "/getSubject", method = RequestMethod.GET)
    public NftResponse<List<String>> getSubject() {
        return bookManagerService.getSubject();
    }

    @ApiOperation(value="获取分册信息")
    @RequestMapping(value = "/getDispart", method = RequestMethod.GET)
    public NftResponse<List<String>> getDispart() {
        return bookManagerService.getDispart();
    }

    @ApiOperation(value="获取图书列表信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public NftResponse<Paginator<BookResp>> getBookList(@RequestParam("title") String title,
                                                        @RequestParam("grade") String grade,
                                                        @RequestParam("subject") String subject,
                                                        @RequestParam("dispart") String dispart,
                                                        @RequestParam("userId") String userId,
                                                        @RequestParam("pageNo") Integer pageNo,
                                                        @RequestParam("pageSize") Integer pageSize) {
        BookReq query = new BookReq();
        try {
            query.setTitle(java.net.URLDecoder.decode(title, "UTF-8"));
            query.setGrade(java.net.URLDecoder.decode(grade, "UTF-8"));
            query.setSubject(java.net.URLDecoder.decode(subject, "UTF-8"));
            query.setDispart(java.net.URLDecoder.decode(dispart, "UTF-8"));
            query.setUserId(Long.valueOf(userId));
            query.setPageNo(pageNo);
            query.setPageSize(pageSize);
        }catch (Exception e){}
        return bookManagerService.getBookList(query);
    }

    @ApiOperation(value="获取图书介绍信息")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public NftResponse<BookResp> getBookInfo(@RequestParam("id") Long id) {
        return bookManagerService.getBookInfoById(id);
    }

    @ApiOperation(value="获取图书内容详细")
    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    public NftResponse<List<BookDetailDto>> getBookDetailInfo(@RequestParam("id") Long id) {
        return bookManagerService.getBookDetailInfoById(id);
    }

    @ApiOperation(value="获取图书内容解析详细")
    @RequestMapping(value = "/getDetailJx", method = RequestMethod.GET)
    public NftResponse<List<BookContentDto>> getBookDetailJxInfo(@RequestParam("id") Long id, @RequestParam("jxkey") String jxkey) {
        return bookManagerService.getBookDetailJxInfoById(id, jxkey);
    }

    @ApiOperation(value="保存作业拍摄内容")
    @RequestMapping(value = "/saveWorkPhone", method = RequestMethod.POST)
    public NftResponse<Boolean> saveWorkPhone(HttpServletRequest request, @RequestParam(value = "file")MultipartFile file) {
        return bookManagerService.saveWorkPhone(request,file);
    }

    @ApiOperation(value="获取图书")
    @RequestMapping(value = "/getBook", method = RequestMethod.GET)
    public NftResponse<Paginator<BookResp>> getBook(@RequestParam("userId") Long userId,
                                                    @RequestParam("pageNo") Integer pageNo,
                                                    @RequestParam("pageSize") Integer pageSize) {
        return bookManagerService.getBookInfos(userId, pageNo, pageSize);
    }

    @ApiOperation(value="添加图书")
    @RequestMapping(value = "/putBook", method = RequestMethod.PUT)
    public NftResponse<Boolean> putBook(@RequestParam("id") Long id, @RequestParam("userId") Long userId) {
        UserBookReq query = new UserBookReq();
        query.setTaskId(id);
        query.setUserId(userId);
        return bookManagerService.putBookInfo(query);
    }

    @ApiOperation(value="获取班级")
    @RequestMapping(value = "/getClass", method = RequestMethod.GET)
    public NftResponse<List<String>> getClass(@RequestParam("userId") Long userId) {
        return bookManagerService.getClassInfos(userId);
    }

    @ApiOperation(value="加入班级")
    @RequestMapping(value = "/putClass", method = RequestMethod.PUT)
    public NftResponse<Boolean> putClass(@RequestParam("userId") Long userId, @RequestParam("classId") Long classId) {
        return bookManagerService.putClassInfo(userId, classId);
    }

    @ApiOperation(value="修改姓名")
    @RequestMapping(value = "/putUserName", method = RequestMethod.PUT)
    public NftResponse<Boolean> putUsername(@RequestParam("userId") Long userId, @RequestParam("userName") String userName) {
        try {
            String updateUserName = java.net.URLDecoder.decode(userName, "UTF-8");
            return bookManagerService.putUsername(userId, updateUserName);
        }catch (Exception e){
            return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", false);
        }
    }

    @ApiOperation(value="修改头像")
    @RequestMapping(value = "/putAvatar", method = RequestMethod.POST)
    public NftResponse<String> putAvatar(HttpServletRequest request, @RequestParam(value = "file", required = false)MultipartFile file) {
        String avatar = bookManagerService.putAvatar(request,file);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "设置成功", avatar);
    }

    @ApiOperation(value="获取微信用户手机号")
    @RequestMapping(value = "/getPhone", method = RequestMethod.GET)
    public NftResponse<String> getPhone(@RequestParam("encryptedData") String encryptedData,
                                        @RequestParam("iv") String iv,
                                        @RequestParam("sessionKey") String sessionKey,
                                        @RequestParam("openid") String openid) {
        String phonenumber = WxCode.getInstance().getMinPhonenumber(encryptedData, iv, sessionKey);
        if (null!=phonenumber&&!"".equals(phonenumber)) {
            bookManagerService.putOpenId(phonenumber, openid);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", phonenumber);
    }

    @ApiOperation(value="获取微信小程序授权")
    @RequestMapping(value = "/putOpenid", method = RequestMethod.GET)
    public NftResponse<com.alibaba.fastjson.JSONObject> putOpenid(@RequestParam("code") String code) {
        com.alibaba.fastjson.JSONObject result = WxCode.getInstance().getMinOpenId(code);
        String openId = result.getString("openid");
        NftResponse<SysUserResp> rep = bookManagerService.getUserInfo(openId);
        SysUserResp u = rep.getData();
        Optional<SysUserResp> optional = Optional.ofNullable(u);
        if (optional.isPresent()) {
            result = (JSONObject)com.alibaba.fastjson.JSONObject.toJSON(u);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", result);
    }

    @ApiOperation(value="微信用户信息")
    @RequestMapping(value = "/wxUser", method = RequestMethod.GET)
    public NftResponse<SysUserResp> wxUser(@RequestParam("openId") String openId) {
        return bookManagerService.getUserInfo(openId);
    }

    @ApiOperation(value="获取错题本")
    @RequestMapping(value = "/getErrorQ", method = RequestMethod.GET)
    public NftResponse<List<UserBookDetailResp>> getErrorQ(@RequestParam("taskId") Long taskId,
                                                           @RequestParam("userId") Long userId,
                                                           @RequestParam("status") String status) {
        return bookManagerService.getErrorQ(taskId, userId, status);
    }
}
